/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * HttpUtils.java
 *
 */
package com.gfinal.map.util;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author Gao Yudong (gyd@nbicc.com)
 * @date 2015-2-25
 */
public class HttpUtils {
	private static final String TAG = HttpUtils.class.getSimpleName();

	public static String doGet(String url) {
		String response = null;
		CloseableHttpResponse res = null;
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpget = new HttpGet(url);
		try {
			res = client.execute(httpget);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				response = IOUtils.toString(entity.getContent(), "UTF-8");
				EntityUtils.consume(entity);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			try {
				res.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return response;
	}
}
